package com.shanshui.viewpager2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.FragmentUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.shanshui.viewpager2.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        binding.vpHomeContent.setOffscreenPageLimit(2);
        Adapter adapter = new Adapter(getSupportFragmentManager(), getLifecycle());
        binding.vpHomeContent.setAdapter(adapter);
        adapter.setdate();
        new TabLayoutMediator(binding.tabLayout, binding.vpHomeContent, true, false, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText("dsf" + position);
            }
        }).attach();
//        binding.vpHomeContent.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                super.onPageSelected(position);
//                Log.e(TAG, "onPageSelected: ");
//            }
//        });
    }

    public static class Adapter extends FragmentStateAdapter{
        private List<String> mStringList = new ArrayList<>();
        private void setdate() {
            for (int i = 0; i < 8; i++) {
                mStringList.add("sd");
            }
            notifyDataSetChanged();
        }

        public Adapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return BlankFragment.newInstance("", "");
        }

        @Override
        public int getItemCount() {
            return mStringList.size();
        }
    }
}